package com.netty;

import io.netty.channel.Channel;

import java.util.*;

/**
 * @Author Zhangjy
 * @Date 2021/6/29 16:01
 * @Desc Session管理器，维护手机号和Channel的关系
 */
public class ChannelManager {

    /**
     * <CellPhoneNumber-Channel>
     */
    private static final Map<String, Channel> SESSION_MAP;

    static {
        SESSION_MAP = new HashMap<>();
    }

    /**
     * 根据手机号保存Session
     *
     * @param cellPhoneNumber
     * @param channel
     */
    public static void put(String cellPhoneNumber, Channel channel) {
        SESSION_MAP.put(cellPhoneNumber, channel);
    }

    /**
     * 根据手机号获取TerminalSession
     *
     * @param cellPhoneNumber
     * @return
     */
    public static Optional<Channel> get(String cellPhoneNumber) {
        return Optional.ofNullable(SESSION_MAP.get(cellPhoneNumber));
    }

    /**
     * 根据手机号获取删除TerminalSession
     *
     * @param cellPhoneNumber
     * @return
     */
    public static void remove(String cellPhoneNumber) {
        SESSION_MAP.remove(cellPhoneNumber);
    }

    public static List<Channel> listAll() {
        return new ArrayList<>(SESSION_MAP.values());
    }

}
